package com.nefu.softlab.xjwc.controller;

import com.nefu.softlab.xjwc.model.Activity;
import com.nefu.softlab.xjwc.model.vo.ActivityVo;
import com.nefu.softlab.xjwc.service.ActivityService;
import com.nefu.softlab.xjwc.utils.JSONResult;
import com.nefu.softlab.xjwc.utils.JsonUtil;
import com.nefu.softlab.xjwc.utils.LoggerUtil;
import com.nefu.softlab.xjwc.utils.XJWCException;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

/**
 * @author LiXiwen
 * @date 2020/2/7 18:13
 */
@RestController
@CrossOrigin(origins = "*", allowCredentials = "true", allowedHeaders = "*")
@Slf4j
public class ActivityApi {

    private final ActivityService activityService;
    private final LoggerUtil logger;

    @Autowired
    public ActivityApi(ActivityService activityService, LoggerUtil logger) {
        this.activityService = activityService;
        this.logger = logger;
    }

    @RequiresRoles("admin")
    @PostMapping("/activity")
    public JSONResult addActivity(@RequestBody ActivityVo activity) throws XJWCException {
        if (StringUtils.isEmpty(activity)) {
            return JSONResult.errorMessage("输入无效, 请重新输入!");
        }
        log.info("POST addActivity : " + JsonUtil.getJsonString(activity));
        logger.info("POST addActivity : " + JsonUtil.getJsonString(activity));
        return activityService.addActivity(activity);
    }

    @RequiresRoles("doctor")
    @GetMapping("/activity")
    public JSONResult selectAllActivity(Activity activity) throws XJWCException {
        if (StringUtils.isEmpty(activity)) {
            return JSONResult.errorMessage("输入无效, 请重新输入!");
        }
        logger.info("GET selectAllActivity : " + JsonUtil.getJsonString(activity));
        return activityService.selectAllActivity(activity);
    }

    @RequiresRoles("admin")
    @PutMapping("/activity")
    public JSONResult updateActivity(@RequestBody ActivityVo activity) throws XJWCException {
        if (StringUtils.isEmpty(activity)) {
            return JSONResult.errorMessage("输入无效, 请重新输入!");
        }
        logger.info("PUT updateActivity : " + JsonUtil.getJsonString(activity));
        log.info("PUT updateActivity : " + JsonUtil.getJsonString(activity));
        return activityService.updateActivity(activity);
    }

    @RequiresRoles("admin")
    @DeleteMapping("/activity/{activityId}")
    public JSONResult deleteActivity(@PathVariable Integer activityId) throws XJWCException {
        if (null == activityId || 0 >= activityId) {
            return JSONResult.errorMessage("输入无效, 请重新输入!");
        }
        logger.info("DELETE deleteActivity : " + activityId);
        return activityService.deleteActivity(activityId);
    }


    @RequiresRoles("doctor")
    @GetMapping("/activity/now")
    public JSONResult activityIsNow(@RequestParam("userId") String userId) throws XJWCException {
        if (StringUtils.isEmpty(userId)) {
            return JSONResult.errorMessage("输入无效, 请重新输入!");
        }
        logger.info("GET activityIsNow : " + userId);
        return activityService.selectInTimeActivity(userId);
    }


    @RequiresRoles("admin")
    @GetMapping("/allActivity")
    public JSONResult getAllActivity() throws XJWCException {
        logger.info("GET getAllActivity : ");
        return activityService.getAllActivity();
    }






}
